package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.IApAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-05-15
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements IApAuthorService {

    /**
     * 保存文章作者用户
     *
     * @param entity
     * @return
     */
    @Override
    public ResponseResult<ApAuthor> saveApAuthor(ApAuthor entity) {
        //1. 查询当前APP用户是否存在作者记录
        LambdaQueryWrapper<ApAuthor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApAuthor::getUserId, entity.getUserId());
        ApAuthor one = this.getOne(queryWrapper);
        //2.封装保存作者对象属性
        if (one == null) {
            //3.执行保存  shfit+alt+箭头
            this.save(entity);
            return ResponseResult.okResult(entity);
        }
        return ResponseResult.okResult(one);
    }
}
